package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsAppVer;
import com.yuke.cloud.service.cmc.service.CmsAppVerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wg on 2019/07/23.
 */
@Api(value = "WEB - CmsAppVerController", tags = "处理CmsAppVer相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/app_ver")
public class CmsAppVerController extends BaseController {
    @Resource
    private CmsAppVerService cmsAppVerService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsAppVer", value = "该数据记录的POJO") @RequestBody CmsAppVer cmsAppVer) {

        // 将version_name转换成大写
        if (cmsAppVer.getVersionName() != null) {
            cmsAppVer.setVersionName(cmsAppVer.getVersionName().toUpperCase());
        }

        cmsAppVerService.save(cmsAppVer);
        return WrapMapper.ok(cmsAppVer.getAppVerId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsAppVerService.deleteById(id);
        return WrapMapper.ok();
    }

    @DeleteMapping("/ids")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    public Wrapper deleteByIds(@ApiParam(name = "ids", value = "要删除的APP版本信息ID集") @RequestBody String appVerIds) {
        cmsAppVerService.deleteByIds(appVerIds);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsAppVer", value = "需要修改的内容") @RequestBody CmsAppVer cmsAppVer) {

        // 将version_name转换成大写
        if (cmsAppVer.getVersionName() != null) {
            cmsAppVer.setVersionName(cmsAppVer.getVersionName().toUpperCase());
        }

        cmsAppVerService.update(cmsAppVer);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsAppVer> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsAppVer cmsAppVer = cmsAppVerService.findById(id);
        return WrapMapper.ok(cmsAppVer);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsAppVer>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                             @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                             @ApiParam(name = "appType", value = "APP类型") @RequestParam(required = false) Byte appType,
                                             @ApiParam(name = "appProject", value = "APP类型") @RequestParam(required = false) Byte appProject) {
        PageHelper.startPage(page, size);

        PageHelper.orderBy("update_time desc");
        List<CmsAppVer> list = null;

        Example example = new Example(CmsAppVer.class);
        Example.Criteria criteria = example.createCriteria();

        if (appType != null ) {
            criteria.andEqualTo("appType", appType);
        }

        if (appProject != null) {
            criteria.andEqualTo("appProject", appProject);
        }

        list = cmsAppVerService.selectByExample(example);

        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据appType及versionCode判断是否有相同版本存在")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody CmsAppVer cmsAppVer) {

        Example example = new Example(CmsAppVer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appType", cmsAppVer.getAppType());
        if (cmsAppVer.getAppProject() != null) {
            criteria.andEqualTo("appProject", cmsAppVer.getAppProject());
        }
        if (cmsAppVer.getAppType() == 1) {  // 安卓

            String versionCode = "";
            String versionName = "";

            if (PublicUtil.isNotEmpty(cmsAppVer.getVersionCode())) {
                versionCode = cmsAppVer.getVersionCode().toString();
            }
            if (PublicUtil.isNotEmpty(cmsAppVer.getVersionName())) {
                versionName = cmsAppVer.getVersionName();
            }

            criteria.andCondition("(version_code='" + versionCode + "' OR version_name='" + versionName + "')");
        } else {
            if (cmsAppVer.getVersionName() != null) {
                criteria.andEqualTo("versionName", cmsAppVer.getVersionName());
            }

            if (cmsAppVer.getVersionCode() != null) {
                criteria.andEqualTo("versionCode", cmsAppVer.getVersionCode());
            }
        }

        if (cmsAppVer.getAppVerId() != null) {
            criteria.andNotEqualTo("appVerId", cmsAppVer.getAppVerId());
        }
        int count = cmsAppVerService.selectCountByExample(example);
        return WrapMapper.ok(count);
    }
}
